<?php
namespace Admin\Controller;
use Think\Controller;
class ApplyController extends Controller {


    /**
     *
     * 出金
     */
    public function drawing(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}

        $fo = I('fo') ? strtotime(I('fo')) : "";
        $to = I('to') ? strtotime(I('to')) : "";
        $status = I('status') ? I('status') : "";
        $mt4 = I('mt4') ? I('mt4') : "";
        $where = '';
        if(!empty($mt4)){
            $where['mt4'] = $mt4;
        }
        if(!empty($status)){
            $where['status'] = $status;
        }
        if(!empty($fo) &&  !empty($to)){
            $where['gold_time'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if(empty($where)){
            $count = M('gold')->count();
            $Page = new \Think\Page($count, 15);
            $show = $Page->show();// 分页显示输出
            $data = M('gold')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        } else  {
            $count = M('gold')->where($where)->count();
            $Page = new \Think\Page($count, 20);
            $Page->parameter['mt4']=$mt4;
            $Page->parameter['fo']=date('Y-m-d',$fo);
            $Page->parameter['to']=date('Y-m-d',$to);
            $Page->parameter['status']=$status;
            $show = $Page->show();// 分页显示输出
            $data = M('gold')->order('id desc')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        }
        foreach ($data as $k=>$v){
            if($v['status']==1){
                $data[$k]['status']="<font color='#bc8f8f'>待审核</font>";
            }else if($v['status']==2){
                $data[$k]['status']="<font color='#556b2f'>处理中</font>";
            }else if($v['status']==3){
                $data[$k]['status']="<font color='green'>已受理</font>";
            }else if($v['status']==4) {
                $data[$k]['status'] = "<font color='red'>未通过</font>";
            }
        }
        $this->assign('to',date('Y-m-d',$to));
        $this->assign('fo',date('Y-m-d',$fo));
        $this->assign('status',$status);
        $this->assign('mt4',$mt4);
        $this->assign('page',$show);
        $this->assign('data',$data);
        //$this->assign('set',$set);
        $this->display();
    }
    //查看详情
    public function drawing_OneData(){
        $admin = session('admin');
        if(empty($admin)){die( "身份信息过期请重新登录");}
        $id = I('id')?I('id'):die( "没有获取到数据，请刷新后再试");
        $data = M('gold')->where(array('id'=>$id))->find();
        if($data['status']==1){
            $data['state']="待审核";
        }else if($data['status']==2){
            $data['state']="处理中";
        }else if($data['status']==3){
            $data['state']="已受理";
        }else if($data['status']==4){
            $data['state']="未通过";
        }
        $this->assign('data',$data);
        $this->display();
    }
    //审核通过
    public function drawing_status(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id')?I('id'):die(array('error'=>"信息错误",'status'=>0));
        $mt4 = I('mt4')?I('mt4'):die(array('error'=>"信息错误",'status'=>0));

        $status_save = M('gold')->where(array('id'=>$id))->save(array('status'=>3));
        if($status_save!=false){
            $user = M('user')->field('user_id,user_email')->where(['user_id'=>$mt4])->find();
            $mould= M('mail_mould')->where(['type'=>10002])->find();
            $msg=str_replace('{1}',$mt4,$mould['content']);
            $send = SendMail($user['user_email'], $mould['title'], htmlspecialchars_decode($msg));
            if($send){
                $this->ajaxReturn(array('msg'=>"审核成功",'status'=>1));
            }else{
                $this->ajaxReturn(array('status'=>1,'msg'=>"审核成功,邮件发送失败"));
            }
        }else{
            $this->ajaxReturn(array('error'=>"审核失败",'status'=>0));
        }
    }
    //审核拒绝 - 理由发送
    public function drawing_refuse(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id')?I('id'):die(array('status'=>0,'error'=>"信息错误"));
        $refuse = I('content')?I('content'):die(array('status'=>0,'error'=>"理由不能为空"));
        $mt4 = I('mt4')?I('mt4'):die(array('status'=>0,'error'=>"信息错误"));
        $status_save = M('gold')->where(array('id'=>$id))->save(array('status'=>4,'reason'=>$refuse));
        if($status_save!=false){
            $user = M('user')->where(['user_id'=>$mt4])->field('user_id,user_email')->find();
            $mould= M('mail_mould')->where(['type'=>10003])->find();
            $msg  = str_replace('{1}',$refuse,$mould['content']);
            $send = SendMail($user['user_email'], $mould['title'], htmlspecialchars_decode($msg));
            if($send){
                $this->ajaxReturn(array('status'=>1,'msg'=>"拒绝成功"));
            }else{
                $this->ajaxReturn(array('status'=>1,'msg'=>"拒绝成功,邮件发送失败"));
            }
        }else{
            $this->ajaxReturn(array('status'=>0,'error'=>"拒绝失败"));
        }
    }
    //删除
    public function gold_delete(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id')?I('id'):die(array('status'=>0,'error'=>"删除失败"));

        $delete = M('gold')->where(array('id'=>$id))->delete();
        if($delete){
            $this->ajaxReturn(array('status'=>1,'msg'=>"删除成功"));
        }else{
            $this->ajaxReturn(array('status'=>0,'error'=>"删除失败"));
        }
    }
    //下载已查询出金数据表
    public function drawing_table(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}

        $fo = I('fo') ? strtotime(I('fo')) : "";
        $to = I('to') ? strtotime(I('to')) : "";
        $status = I('status') ? I('status') : "";
        $mt4 = I('mt4') ? I('mt4') : "";
        $where = '';
        if(!empty($mt4)){
            $where['gold.mt4'] = $mt4;
        }
        if(!empty($status)){
            $where['gold.status'] = $status;
        }
        if(!empty($fo) && !empty($to)){
            $where['gold.gold_time'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if(!empty($where)){
            $csv = new \Think\Csv();
            $table_data = M('gold')->order('id desc')->where($where)
                ->field('gold.*,user.user_name as username,user.name_zh_cn')
                ->join('user on gold.mt4=user.user_id')
                ->select();
            $data=[];
            foreach ($table_data as $k=>$v){
                $data[$k]['id']=$v['id'];                                            //id
                $data[$k]['name_zh_cn']=$v['name_zh_cn'];                            //注册姓名
                $data[$k]['mt4']=$v['mt4'];                                          //mt4账号
                $data[$k]['gold_time']=date("Y-m-d H:i:s",$v['gold_time']);   //申请时间
                $data[$k]['usd']=$v['usd'];                  //提款数额
                $data[$k]['rmb']=$v['rmb'];                  //提款换算后金额(￥)
                $data[$k]['fee']=$v['fee'];                  //手续费(币数)
                if($v['status']==1){
                    $data[$k]['status']="待审核";
                }else if($v['status']==2){
                    $data[$k]['status']="处理中";
                }else if($v['status']==3){
                    $data[$k]['status']="已受理";
                }else if($v['status']==4){
                    $data[$k]['status']="未通过";
                }
                $data[$k]['user_name']=$v['user_name'];             //收款人姓名
                $data[$k]['bank_number']=$v['bank_number'];         //卡号
                $data[$k]['area_2']=$v['area_2'];                   //开户行城市
                $data[$k]['bank']=$v['bank'];                       //银行名称
            }
            $csv_title=array('ID','姓名','MT4账户','申请时间','币数','金额(￥)','手续费(个)','状态','收款人姓名','收款人银行账号','开户行城市','银行名称');
            $csv->put_csv($data, $csv_title, "提现数据表");


        } else {
            echo "请先查询数据后下载";die;
        }
    }
    //修改待审核状态
    public function drawing_state_up(){
        $admin = session('admin');
        if(empty($admin)){ $this->ajaxReturn(array('error'=>"身份信息过期,请重新登录",'status'=>0));exit;}

        $up = M('gold')->where(array('status'=>1))->save(array('status'=>2));
        if($up!==false){
            $this->ajaxReturn(array('msg'=>"修改成功",'status'=>1));
        }else{
            $this->ajaxReturn(array('error'=>"修改状态未成功",'status'=>0));
        }
    }
    //下载待审核的数据
    public function download_wait(){
        $table=M("gold")->where(['gold.status'=>1])
            ->field('gold.*,user.user_name as username,user.name_zh_cn')
            ->join('user on gold.mt4=user.user_id')
            ->select();//查询数据，可以进行处理
        $csv = new \Think\Csv();
        //$set = M('gold_set')->where(array('state'=>1))->find();
        foreach ($table as $k=>$v){
            $data[$k]['id']=$v['id'];                                       //id
            $data[$k]['name_zh_cn']=$v['name_zh_cn'];                            //注册姓名
            $data[$k]['mt4']=$v['mt4'];                                          //mt4账号
            $data[$k]['gold_time']=date("Y-m-d H:i:s",$v['gold_time']);   //申请时间
            $data[$k]['usd']=$v['usd'];                  //提款金额$
            $data[$k]['rmb']=$v['rmb'];                  //提款金额￥
            $data[$k]['fee']=$v['fee'];                  //手续费
            if($v['status']==1){
                $data[$k]['status']="待审核";
            }else if($v['status']==2){
                $data[$k]['status']="处理中";
            }else if($v['status']==3){
                $data[$k]['status']="已受理";
            }else if($v['status']==4){
                $data[$k]['status']="未通过";
            }
            $data[$k]['user_name']=$v['user_name'];             //收款人姓名
            $data[$k]['bank_number']=$v['bank_number'];         //卡号
            $data[$k]['area_2']=$v['area_2'];                   //开户行城市
            $data[$k]['bank']=$v['bank'];                       //银行名称
        }
        $csv_title=array('ID','姓名','MT4账户','申请时间','币额(个)','换算后金额(￥)','手续费(个)','状态','收款人姓名','收款人银行账号','开户行城市','银行名称');
        $csv->put_csv($data, $csv_title, "出金待审核数据");
    }

    /**
     *
     * 入金管理
     */
    public function intoGold(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}

        $fo = I('fo') ? strtotime(I('fo')) : "";
        $to = I('to') ? strtotime(I('to')) : "";
        $status = I('status') ? I('status') : "";
        $mt4 = I('mt4') ? I('mt4') : "";
        if(!empty($mt4)){
            $where['mt4'] = $mt4;
        }
        if(!empty($status)){
            $where['status'] = $status;
        }
        if(!empty($fo) && !empty($to)){
            $where['into_time'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if(empty($where)){
            $count = M('into_gold')->count();
            $Page = new \Think\Page($count, 15);
            $show = $Page->show();// 分页显示输出
            $data = M('into_gold')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        } else  {
            $count = M('into_gold')->where($where)->count();
            $Page = new \Think\Page($count, 20);
            $Page->parameter['mt4']=$mt4;
            $Page->parameter['fo']=date('Y-m-d',$fo);
            $Page->parameter['to']=date('Y-m-d',$to);
            $Page->parameter['status']=$status;
            $show = $Page->show();// 分页显示输出
            $data = M('into_gold')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        }
        foreach ($data as $k=>$v){
            if($v['status']==1){
                $data[$k]['status']="<font color='#bc8f8f'>未付款</font>";
            }else if($v['status']==2){
                $data[$k]['status']="<font color='green'>已到账</font>";
            }else if($v['status']==3){
                $data[$k]['status']="<font color='red'>未到账</font>";
            }
        }
        //当天
        $beginToday =mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday   =mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $oneday = M('into_gold')->where('(into_time between \'' . $beginToday . '\' and  \'' . $endToday . '\') ')->sum('final_money');
        //本月
        $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
        $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
        $monthday = M('into_gold')->where('(into_time between \'' . $beginThismonth . '\' and  \'' . $endThismonth . '\') ')->sum('final_money');
        //上月
        $begin_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
        $end_time = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day')));
        $Last_month = M('into_gold')->where('(into_time between \'' . $begin_time . '\' and  \'' . $end_time . '\') ')->sum('final_money');

        $this->assign('oneday',$oneday);
        $this->assign('monthday',$monthday);
        $this->assign('Last_month',$Last_month);
        $this->assign('to',date('Y-m-d',$to));
        $this->assign('fo',date('Y-m-d',$fo));
        $this->assign('status',$status);
        $this->assign('mt4',$mt4);
        $this->assign('page',$show);
        $this->assign('data',$data);
        $this->display();
    }
    //入金表格下载
    public function intoGold_table(){
        $fo = I('fo') ? strtotime(I('fo')) : "";
        $to = I('to') ? strtotime(I('to')) : "";
        $status = I('status') ? I('status') : "";
        $mt4 = I('mt4') ? I('mt4') : "";
        if(!empty($mt4)){
            $where['mt4'] = $mt4;//账号
        }
        if(!empty($status)){
            $where['status'] = $status;//状态
        }
        if(!empty($fo) && !empty($to)){
            $where['into_time'] = array('between',array($fo,$to));//时间
        }
        if(!empty($where)){
            $csv = new \Think\Csv();
            $table = M('into_gold')->where($where)->order('id desc')->select();
            $data=[];
            foreach ($table as $k=>$v){
                $data[$k]['id']=$v['id'];
                $data[$k]['mt4']=$v['mt4'];
                $data[$k]['modu']=$v['modu'];
                $data[$k]['money']=$v['money'];
                $data[$k]['rate']=$v['rate'];
                $data[$k]['final_money']=$v['final_money'];
                $data[$k]['counter_fee']=$v['counter_fee'];
                $data[$k]['into_time']=date('Y-m-d H:i:s',$v['into_time']);
                if($v['status']==1){
                    $data[$k]['status']="未付款";
                }else if($v['status']==2){
                    $data[$k]['status']="MT4已到账";
                }else if($v['status']==3){
                    $data[$k]['status']="MT4未到账";
                }
            }
            $csv_title=array('ID','MT4账户','入金方式','金额($)','汇率','最终金额($)','手续费','入金时间','状态');
            $csv->put_csv($data, $csv_title, "入金数据");

        }else{
            echo "请先查询数据后下载";die;
        }

    }
    //入金单个详情查看
    public function intoGold_OneData(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(array('msg'=>"没有获取到数据，请刷新",'status'=>1));exit;
        }else{
            $data = M('into_gold')->where(array('id'=>$id))->find();

            if($data['status']==1){
                $data['state']="<font color='#bc8f8f'>未付款</font>";
            }else if($data['status']==2){
                $data['state']="<font color='green'>已到账</font>";
            }else{
                $data['state']="<font color='red'>未到账</font>";
            }
            $data['into_time'] = date("Y-m-d H:i:s",$data['into_time']);
            $this->assign('data',$data);
            $this->display();
        }
    }
    //补单
    public function intoGold_Order(){
        $admin = session('admin');
        if(empty($admin)){
            $this->ajaxReturn(array('error'=>"身份信息过期，请重新登录",'status'=>0));exit;
        }
        if($admin['user_role']<4){
            $this->ajaxReturn(array('error'=>"您没有权限补单",'status'=>0));exit;
        }
        $id = I('id')?I('id'):die(array('error'=>"信息失效,刷新页面再试",'status'=>0));
        $mt4 = I('mt4')?I('mt4'):die(array('error'=>"信息失效,刷新页面再试",'status'=>0));
        $notes = I('notes')?I('notes'):die(array('error'=>"请确保备注信息完整",'status'=>0));        //备注

        if(empty($id) || empty($mt4) ||empty($notes)){$this->ajaxReturn(array('error'=>"请确保信息完整，或刷新页面再试",'status'=>0));exit;}
        //$comment = 'deposit';
        $preg='/^deposit#[0-9]{10}$/';
        if(preg_match($preg,$notes)) {
            $comment1=explode('#',$notes);
            $id = $comment1[1];
            $query = M('into_gold')->where(array('into_id'=>$id))->find();//查询订单是否存在
            if(!empty($query)){
                 $query2 = M('into_gold')->where(array('into_id'=>$id,'into_status'=>2))->find();//在查询订单是否为已成功的订单
                 if(!empty($query2)){
                     $this->ajaxReturn(array('error'=>"您输入的为已成功的订单",'status'=>0));exit;
                 }
                 //修改订单状态和 最终金额=入金的金额
                $up = M('into_gold')->where(array('into_id'=>$id))->save(array('final_money'=>$query['money'],'status'=>2));
                 if($up!=false){//修改成功后在入进MT4
                    $request = "action=bulkchangebalance&logins=$mt4&value=".$query['money']."&comment=$notes\0";
                    $answer=mt4_operation($request);
                    $answer = explode('&', $answer);
                    $data = [];
                    foreach ($answer as $k => $v) {
                        $arr = explode('=', $v);
                        $data[$arr[0]] = $arr[1];
                    }
                    if($data['result']==1){
                        $this->ajaxReturn(array('msg'=>"补单成功",'status'=>1));
                    }else{
                        M('into_gold')->where(array('into_id'=>$id))->save(array('final_money'=>0,'into_status'=>1));
                        $this->ajaxReturn(array('error'=>"补单失败",'status'=>0));
                    }

                 }else{
                    $this->ajaxReturn(array('error'=>"修改出错,请稍后再试",'status'=>0));
                }

             }else{
                 $this->ajaxReturn(array('error'=>"您输入的订单号不正确",'status'=>0));
             }


        }else{
            $this->ajaxReturn(array('error'=>"备注信息不正确",'status'=>0));
        }



    }

    /**
     *
     * 职位申请
     */
    public function positionApply(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $fo = I('fo') ? strtotime(I('fo')) : "";
        $to = I('to') ? strtotime(I('to')) : "";
        $status = I('status') ? I('status') : "";
        $mt4 = I('mt4') ? I('mt4') : "";
        if(!empty($mt4)){
            $where['mt4'] = $mt4;
        }
        if(!empty($status)){
            $where['state'] = $status;
        }
        if(!empty($fo) && !empty($to)){
           $where['apply_time'] = array('between',array($fo,$to.' 23:59:59'));
        }
        $position = M('position')->order('id desc')->select();
        if(empty($where)){
            $count = M('position_apply')->count();
            $Page = new \Think\Page($count, 10);
            $show = $Page->show();// 分页显示输出
            $list = M('position_apply')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        }else {
            $count = M('position_apply')->where($where)->count();
            $Page = new \Think\Page($count, 10);
            $Page->parameter['mt4']=$mt4;
            $Page->parameter['fo']=date('Y-m-d',$fo);
            $Page->parameter['to']=date('Y-m-d',$to);
            $Page->parameter['status']=$status;
            $show = $Page->show();// 分页显示输出
            $list = M('position_apply')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        }
        foreach ($list as $k=>$v){
            $list[$k]['apply_time'] = date('Y-m-d H:i:s',$v['apply_time']);
            if($v['state']>1){
                $list[$k]['hand_time'] = date('Y-m-d H:i:s',$v['hand_time']);
            }
            if($v['state']==1){
                $list[$k]['state']="<font color='#bc8f8f'>待审核</font>";
            }else if($v['state']==2){
                $list[$k]['state']="<font color='green'>已同意</font>";
            }else if($v['state']==3){
                $list[$k]['state']="<font color='red'>已拒绝</font>";
            }
        }
        $this->assign('to',date('Y-m-d',$to));
        $this->assign('fo',date('Y-m-d',$fo));
        $this->assign('status',$status);
        $this->assign('mt4',$mt4);
        $this->assign('data',$list);
        $this->assign('position',$position);
        $this->assign('page',$show);
        $this->display();
    }
    //职位申请详情
    public function position_OneData(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        if(!empty($id)){
            $data = M('position_apply')->where(array('id'=>$id))->find();
            if(!empty($data)){
                $position = M('position')->order('id desc')->select();
                $data['apply_time'] = date('Y-m-d H:i:s', $data['apply_time']);
                $data['hand_time'] = date('Y-m-d H:i:s', $data['hand_time']);
                $data['status'] = $data['state'];
                if($data['state']==1){
                    $data['state']="<font color='#bc8f8f'>待审核</font>";
                }else if($data['state']==2){
                    $data['state']="<font color='green'>已同意</font>";
                }else if($data['state']==3){
                    $data['state']="<font color='red'>已拒绝</font>";
                }
              $this->assign('data',$data);
              $this->assign('position',$position);
              $this->display();
            }else{
                echo "没有数据，请刷新页面再试";exit;
            }
        }else{
            $this->ajaxReturn(array('error'=>"信息获取失败，请刷新页面再试",'status'=>0));exit;
        }
    }
    //职位申请同意
    public function position_agree(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}

        $id = I('id');
        $mt4 = I('mt4');
        $email = I('email')?I('email'):die();
        $position = I('position')?I('position'):die();

        //var_dump(I(''));die;
        if(empty($id) || empty($mt4) || empty($email) || empty($position)){
            $this->ajaxReturn(array('error'=>"信息不完整，请重新提交",'status'=>0));exit;
        }
        M()->startTrans(); //开启事物
        $position_save = M('user')->where(array('user_id'=>$mt4))->save(array('position'=>$position));
        $apply_save = M('position_apply')->where(array('id'=>$id))->save(array('state'=>2,'hand_time'=>time()));

        if($position_save && $apply_save){
            M()->commit();
//            $user = M('user')->field('user_id,user_email')->where(['user_id'=>$mt4])->find();
            $mould= M('mail_mould')->where(['type'=>10006])->find();
            $msg  = str_replace('{1}',$mt4,$mould['content']);

            $send = SendMail($email, $mould['title'], htmlspecialchars_decode($msg));
            if($send==1){
                $this->ajaxReturn(array('msg'=>"已成功同意申请",'status'=>1));
            }else{
                $this->ajaxReturn(array('msg'=>"已成功同意申请,但是邮件发送失败",'status'=>1));
            }

        }else{
            M()->rollback();
            $this->ajaxReturn(array('error'=>"修改职位失败",'status'=>0));
        }


    }
    //职位申请拒绝
    public function position_refuse(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        $mt4 = I('mt4');
        $email = I('email');
        $refuse = I('refuse')?I('refuse'):die("拒绝理由不能为空");
        if(empty($id) || empty($mt4) || empty($email) ||empty($refuse)){
            $this->ajaxReturn(array('error'=>"信息不完整，请重新提交",'status'=>0));exit;
        }
        $apply_save = M('position_apply')->where(array('id'=>$id))->save(array('state'=>3,'hand_time'=>time(),'refuse'=>$refuse));

        if($apply_save!=false){
            //$user = M('user')->field('user_id,user_email')->where(['user_id'=>$mt4])->find();
            $mould= M('mail_mould')->where(['type'=>10007])->find();
            $msg  = str_replace(array('{1}','{2}'),array($mt4,$refuse),$mould['content']);
            $send = SendMail($email, $mould['title'], htmlspecialchars_decode($msg));
            if($send){
                $this->ajaxReturn(array('msg'=>"拒绝成功",'status'=>1));
            }else{
                $this->ajaxReturn(array('msg'=>"拒绝成功,拒绝理由发送失败。",'status'=>1));
            }

        }else{
            $this->ajaxReturn(array('error'=>"拒绝失败。",'status'=>0));
        }
    }


    /**
     *
     * 杠杆申请
     */
    public function leverApply(){
        $admin =session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $fo = I('fo') ? strtotime(I('fo')) : "";
        $to = I('to') ? strtotime(I('to')) : "";
        $status = I('status') ? I('status') : "";
        $mt4 = I('mt4') ? I('mt4') : "";
        if(!empty($mt4)){
            $where['mt4'] = $mt4;
        }
        if(!empty($status)){
            $where['state'] = $status;
        }
        if(!empty($fo) && !empty($to)){
            $where['apply_time'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if(empty($where)){
            $count = M('lever_apply')->count();
            $Page = new \Think\Page($count, 10);
            $show = $Page->show();// 分页显示输出
            $list = M('lever_apply')->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        }else {
            $count = M('lever_apply')->where($where)->count();
            $Page = new \Think\Page($count, 10);
            $Page->parameter['mt4']=$mt4;
            $Page->parameter['fo']=date('Y-m-d',$fo);
            $Page->parameter['to']=date('Y-m-d',$to);
            $Page->parameter['status']=$status;
            $show = $Page->show();// 分页显示输出
            $list = M('lever_apply')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        }
        foreach ($list as $k=>$v){
            $list[$k]['apply_time'] = date('Y-m-d H:i:s',$v['apply_time']);
            if($v['state']>1){
                $list[$k]['hand_time'] = date('Y-m-d H:i:s',$v['hand_time']);
            }
            if($v['state']==1){
                $list[$k]['state']="<font color='#bc8f8f'>待审核</font>";
            }else if($v['state']==2){
                $list[$k]['state']="<font color='green'>已同意</font>";
            }else if($v['state']==3){
                $list[$k]['state']="<font color='red'>已拒绝</font>";
            }
        }

        $this->assign('to',date('Y-m-d',$to));
        $this->assign('fo',date('Y-m-d',$fo));
        $this->assign('status',$status);
        $this->assign('mt4',$mt4);
        $this->assign('data',$list);
        $this->assign('page',$show);
        $this->display();
    }
    //申请详情
    public function lever_OneData(){
        $id = I('id');
        if(empty($id)){echo "操作错误，请重试";exit;}
        $data = M('lever_apply')->where(array('id'=>$id))->find();
        if(empty($data)){
           echo "没有获取到信息，请刷新页面再试"; exit;
        }else{
            $data['apply_time'] = date('Y-m-d H:i:s',$data['apply_time']);
            if($data['state']>1){
                $data['hand_time'] = date('Y-m-d H:i:s',$data['hand_time']);
            }
            $data['status']=$data['state'];

            if($data['state']==1){
                $data['state']="<font color='#bc8f8f'>待审核</font>";
            }else if($data['state']==2){
                $data['state']="<font color='green'>已同意</font>";
            }else if($data['state']==3){
                $data['state']="<font color='red'>已拒绝</font>";
            }

            $this->assign('data',$data);
            $this->display();
        }
    }
    //同意申请
    public function lever_agree(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
       $id =I('id')?I('id'):$this->ajaxReturn(array('msg'=>"请确认信息完整",'status'=>0));
       $data = M('lever_apply')->where(['id'=>$id])->find();
       if(empty($data)){$this->ajaxReturn(array('msg'=>"未知错误",'status'=>0));exit;}
       $mt4 = $data['mt4'];
       $email = $data['user_email'];
       $lever = $data['apply_lever'];
        //修改MT4上的杠杆
        $request2="action=modifyaccount&login=$mt4&leverage=$lever\0";
        $return=mt4_operation($request2);
        parse_str($return,$res);
        M()->startTrans(); //开启事物
        $lever_save = M('user')->where(array('user_id'=>$mt4))->save(array('level'=>$lever));         //修改用户杠杆
        $apply_save = M('lever_apply')->where(array('id'=>$id))->save(array('state'=>2,'hand_time'=>time())); //修改申请表状态
        if($res['result']==1 || !empty($lever_save) || !empty($apply_save)){
            M()->commit(); //提交实物
            //发送邮件
            $mould= M('mail_mould')->where(['type'=>10008])->find();
            $msg  = str_replace('{1}',$mt4,$mould['content']);
            $send = SendMail($email, $mould['title'], htmlspecialchars_decode($msg));
            //$send=1;
            if($send){
                $this->ajaxReturn(array('msg'=>"已成功同意申请",'status'=>1));
            }else{
                $this->ajaxReturn(array('msg'=>"已成功同意申请,但邮件发送失败",'status'=>1));
            }

        }else{
            M()->rollback();
            $this->ajaxReturn(array('error'=>"修改职位失败",'status'=>0));
        }

    }
    //杠杆申请拒绝
    public function lever_refuse(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $id = I('id');
        $data = M('lever_apply')->where(['id'=>$id])->find();
        $mt4 = $data['mt4'];
        $email = $data['user_email'];
        $refuse = I('refuse')?I('refuse'):die("拒绝理由不能为空");
        if(empty($id) || empty($mt4) || empty($email) ||empty($refuse)){
            $this->ajaxReturn(array('error'=>"信息不完整，请重新提交",'status'=>0));exit;
        }
        //修改申请状态
        $apply_save = M('lever_apply')->where(array('id'=>$id))->save(array('state'=>3,'hand_time'=>time(),'refuse'=>$refuse));
        if($apply_save!=false){
            //修改成功发送邮件
            //$user = M('user')->field('user_id,user_email')->where(['user_id'=>$mt4])->find();
            $mould= M('mail_mould')->where(['type'=>10013])->find();
            $msg  = str_replace('{1}',$refuse,htmlspecialchars_decode($mould['content']));
            $send = SendMail($email, $mould['title'], $msg);
            //$send=1;
            if($send){
                $this->ajaxReturn(array('msg'=>"拒绝成功",'status'=>1));
            }else{
                $this->ajaxReturn(array('msg'=>"拒绝成功,拒绝理由发送失败。",'status'=>1));
            }
        }else{
            $this->ajaxReturn(array('error'=>"拒绝失败。",'status'=>0));
        }


    }

    /***
     *
     * //审核列表
     */
    public function examineList(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $fo = I('fo') ? strtotime(I('fo')) : "";
        $to = I('to') ? strtotime(I('to')) : "";
        $status = I('status') ? I('status') : "";
        $mt4 = I('mt4') ? I('mt4') : "";
        if(!empty($mt4)){
            $where['user_id'] = $mt4;
        }
        if(!empty($status)){
            $where['status'] = $status;
        }
        if(!empty($fo) && !empty($to)){
            $where['img_apply_time'] = array('between',array($fo,$to.' 23:59:59'));
        }
        if(empty($where)){
            $count = M('user')->where(array('status'=>2))->count();
            $Page = new \Think\Page($count,20);
            $show = $Page->show();// 分页显示输出
            $list = M('user')->where(array('status'=>2))->order('user_id desc')
                ->field('user_id,name_zh_cn,user_cards,user_email,province,city,img_apply_time,front_img,behind_img,hand_img,status')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
        }else{
            $count = M('user')->where($where)->count();
            $Page = new \Think\Page($count, 20);
            $Page->parameter['mt4']=$mt4;
            $Page->parameter['fo']=date('Y-m-d',$fo);
            $Page->parameter['to']=date('Y-m-d',$to);
            $Page->parameter['status']=$status;
            $show = $Page->show();// 分页显示输出
            $list = M('user')->where($where)->order('user_id desc')
                ->field('user_id,name_zh_cn,user_cards,user_email,province,city,img_apply_time,front_img,behind_img,hand_img,status')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
        }
        foreach ($list as $k=>$v){
            if($v['status']==1){
                $list[$k]['status']="<font color='#bc8f8f'>未上传</font>";
            }else if($v['status']==2){
                $list[$k]['status']="<font color='blue'>已申请</font>";
            }else if($v['status']==3){
                $list[$k]['status']="<font color='green'>已核实</font>";
            }else if($v['status']==4){
                $list[$k]['status']="<font color='red'>已拒绝</font>";
            }
            if($v['status']>1){
                $list[$k]['img_apply_time']=date('Y-m-d H:i:s',$v['img_apply_time']);
            }else{
                $list[$k]['img_apply_time']='';
            }
        }
        $this->assign('to',date('Y-m-d',$to));
        $this->assign('fo',date('Y-m-d',$fo));
        $this->assign('status',$status);
        $this->assign('mt4',$mt4);
        $this->assign('data',$list);
        $this->assign('page',$show);
        $this->display();
    }
    //资料详情
    public function examine_OneData(){
        $id = I('id');
        if(empty($id)){
            echo "请刷新页面重新审核";die;
        }
        $data= M('user')->where(array('user_id'=>$id))
            ->field('user_id,user_email,user_phone,img_apply_time,front_img,behind_img,hand_img,status')
            ->find();
        $this->assign('data',$data);
        $this->display();
    }

    /***
     *
     * 工单处理
     *
     */
    //工单列表
    public function work_order(){
        $admin = session('admin');
        if(empty($admin)){echo "身份信息过期请重新登录";exit;}
        $fo = I('fo') ? strtotime(I('fo')) : "";
        $to = I('to') ? strtotime(I('to')) : "";
        $status = I('status') ? I('status') : "";
        $mt4 = I('mt4') ? I('mt4') : "";
        if(!empty($mt4)){
            $where['user_id'] = $mt4;
        }
        if(!empty($status)){
            $where['state'] = $status;
        }
        if(!empty($fo) && !empty($to)){
            $where['time'] = array('between',array($fo,$to.' 23:59:59'));
        }

        if(empty($where)){
            $count =  M('work_order')->count();
            $Page = new \Think\Page($count, 8);
            $order_list = M('work_order')
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->order('id desc')
                ->select();
            $show = $Page->show();// 分页显示输出
        }else{
            $count =  M('work_order')->where($where)->count();
            $Page = new \Think\Page($count, 10);
            $Page->parameter['mt4']= $mt4;
            $Page->parameter['fo']=date('Y-m-d',$fo);
            $Page->parameter['to']=date('Y-m-d',$to);
            $Page->parameter['status']=$status;
            $order_list = M('work_order')->where($where)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->order('id desc')
                ->select();
            $show = $Page->show();// 分页显示输出
        }
        foreach ($order_list as $k=>$v){
            if($v['type']==1){
                $order_list[$k]['type']="账号类";
            }elseif($v['type']==2){
                $order_list[$k]['type']="资金类";
            }elseif($v['type']==3){
                $order_list[$k]['type']="操作类";
            }elseif($v['type']==4){
                $order_list[$k]['type']="申请类";
            }
            if($v['state']==1){
                $order_list[$k]['sys_state']="等待处理";
                $order_list[$k]['speed']="0%";
            }else if($v['state']==2){
                $order_list[$k]['sys_state']="处理中";
                $order_list[$k]['speed']="50%";
            }elseif($v['state']==3){
                $order_list[$k]['sys_state']="已完成";
                $order_list[$k]['speed']="100%";
            }else{
                $order_list[$k]['sys_state']="已撤销";
                $order_list[$k]['speed']="100%";
            }
            $order_list[$k]['time']=timeFormat($v['time']);//date('Y-m-d H:i:s',$v['time']);
        }
        $this->assign('fo',date('Y-m-d H;i:s',$fo));
        $this->assign('to',date('Y-m-d H;i:s',$to));
        $this->assign('status',$status);
        $this->assign('page',$show);
        $this->assign('list',$order_list);
        $this->display();
    }
    //工单详情
    public function work_order_detail(){
        $id =I('id')?I('id'):die("没有信息,请重试");
        $work = M('work_order')->where(array('id'=>$id))->find();
        $data = M('order_reply')->where(array('order_id'=>$id))->select();
        if($work['type']==1){
            $work['type']="账号类";
        }elseif ($work['type']==2){
            $work['type']="资金类";
        }elseif ($work['type']==3){
            $work['type']="操作类";
        }elseif ($work['type']==4){
            $work['type']="申请类";
        }
        if($work['state']==1){
            $work['state_w']="等待处理";
            $work['state_jd'] = '0%';
        }elseif($work['state']==2){
            $work['state_w']="处理中";
            $work['state_jd'] = '50%';
        }elseif($work['state']==3){
            $work['state_w']="已完成";
            $work['state_jd'] = '100%';
        }elseif($work['state']==4){
            $work['state_w']="已撤销";
            $work['state_jd'] = '0%';
        }
        $work['time'] = timeFormat($work['time']);//date('Y年m月d日 H:i:s',$work['time']);
        foreach ($data as $k=>$v){
            if($v['sys_state']==1){
                $data[$k]['state']="已提交";
            }elseif($v['sys_state']==2){
                $data[$k]['state']="处理中";
            }elseif($v['state']==3){
                $data[$k]['state']="订单已完成";
            }else{
                $data[$k]['state']="已撤销";
            }

            $data[$k]['time']= timeFormat($v['time']);//date('Y年m月d日 H:i:s',$v['time']);
        }

        $user_img = M('user')->where(['user_id'=>$work['user_id']])->field('image')->find();
        $this->assign('img',$user_img);
        $this->assign('data',$data);
        $this->assign('work',$work);
        $this->display();

    }
    //接收工单
    public function receive_order(){
        $admin = session('admin');
        if(empty($admin)){$this->ajaxReturn(array('error'=>"身份信息过期请重新登录",'status'=>0));exit;}

        $id = I('id')?I('id'):die('信息失效,请刷新重试');
        $order = M('work_order')->where(['id'=>$id])->find();
        if(!empty($order)){
            $update = M('work_order')->where(['id'=>$id])->save(['state'=>2]);
            if($update!=false){
                $data['k_id'] = $order['user_id'];
                $data['order_id'] = $order['id'];
                $data['content'] = "系统已收到您的工单,正在为您处理,请稍等";
                $data['sys_state'] = 2;
                $data['time'] = time();
                $add = M('order_reply')->add($data);
                if($add){
                    $this->ajaxReturn(array('msg'=>"接收成功",'status'=>1));
                }else{
                    M('work_order')->where(['id'=>$id])->save(['state'=>1]);
                    $this->ajaxReturn(array('error'=>"接收失败",'status'=>0));
                }
            }else{
                $this->ajaxReturn(array('error'=>"状态修改失败",'status'=>0));
            }
        }else{
            $this->ajaxReturn(array('error'=>"没有数据",'status'=>0));
        }

    }
    //完成工单
    public function complete_order(){
        $admin = session('admin');
        if(empty($admin)){$this->ajaxReturn(array('error'=>"身份信息过期请重新登录",'status'=>0));exit;}
        $id = I('id')?I('id'):die($this->ajaxReturn(array('error'=>"没有数据",'status'=>0)));
        $content = I('content')?I('content'):die($this->ajaxReturn(array('error'=>"回复必填",'status'=>0)));
        $order = M('work_order')->where(['id'=>$id])->find();
        if(!empty($order)){
            $update = M('work_order')->where(['id'=>$id])->save(['state'=>3]);
            if($update!=false){
                $data['k_id'] = $order['user_id'];
                $data['order_id'] = $order['id'];
                $data['content']  =  $content;
                $data['sys_state'] = 3;
                $data['time'] = time();
                $add = M('order_reply')->add($data);
                if($add){
                    $this->ajaxReturn(array('msg'=>"成功",'status'=>1));
                    $mail=M('mail_mould')->where(['type'=>10015])->find();
                    $msg  = str_replace('{1}', $order['title'],$mail['content']);
                    $send = SendMail($order['email'], $mail['title'], htmlspecialchars_decode($msg));
                }else{
                    M('work_order')->where(['id'=>$id])->save(['state'=>2]);
                    $this->ajaxReturn(array('error'=>"失败",'status'=>0));
                }
            }else{
                $this->ajaxReturn(array('error'=>"状态修改失败",'status'=>0));
            }
        }else{
            $this->ajaxReturn(array('error'=>"没有数据",'status'=>0));
        }

    }
}